package com.wlzg.collectiontool.util

import android.app.Dialog
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.Toast
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import java.text.SimpleDateFormat
import java.util.*

class TimePickerUtil(var context: Context,var ls: (date:String?)->Unit) {
     var pvTime: TimePickerView? = null

    init {
        initTvpicker()
    }

    fun getTime(date: Date): String? {

        val format = SimpleDateFormat("yyyy-MM-dd")
        return format.format(date)
    }

    private fun initTvpicker() {
        pvTime = TimePickerBuilder(context, OnTimeSelectListener { date, v ->
            Toast.makeText(context, getTime(date), Toast.LENGTH_SHORT).show()
            Log.i("pvTime", "onTimeSelect")
            ls.invoke(getTime(date))
        })
            .setTimeSelectChangeListener { Log.i("pvTime", "onTimeSelectChanged") }
            .setType(booleanArrayOf(true, true, true, false, false, false))
            .isDialog(true)
            .addOnCancelClickListener { Log.i("pvTime", "onCancelClickListener") }
            .setItemVisibleCount(5)
            .setLineSpacingMultiplier(2.0f)
            .isAlphaGradient(true)
            .setLabel("年", "月", "日", "", "", "")
            .build()

        val mDialog: Dialog = pvTime!!.getDialog()
        if (mDialog != null) {
            val params = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM
            )
            params.leftMargin = 0
            params.rightMargin = 0
            pvTime!!.getDialogContainerLayout().setLayoutParams(params)
            val dialogWindow = mDialog.window
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim) //修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM)
                dialogWindow.setDimAmount(0.3f)
            }
        }
    }
}